package cwinsor.com.leaf_raking;

import java.util.Arrays;
import java.util.List;

public final class StartTime {

	/*
	 * properties
	 */
	private String name;

	/*
	 * getters
	 */
	public final String name() { return name; };

	/*
	 * private constructor
	 */
	private StartTime(String n) { name = n; }


	/*
	 * global instances
	 */
	public static final StartTime T_8_30 = new StartTime("8:30");
	public static final StartTime T_10_00 = new StartTime("10:00");
	public static final StartTime T_10_30 = new StartTime("10:30");
	public static final StartTime T_11_30 = new StartTime("11:30");
	public static final StartTime T_1_00 = new StartTime("1:00");
	public static final StartTime T_2_30 = new StartTime("2:30");
	public static final StartTime T_3_00 = new StartTime("3:00");
	public static final StartTime T_4_00 = new StartTime("4:00");

	/*
	 * global instances as a list (public method)
	 */
	public static final List<StartTime> asList() {
		return instanceList;		
	}

	/*
	 * instances list (private)
	 */
	private static final List<StartTime> instanceList = Arrays.asList(
			T_8_30,
			T_10_00,
			T_10_30,
			T_11_30,
			T_1_00,
			T_2_30,
			T_3_00,
			T_4_00);

	/*
	 * number of instances
	 */
	public static final Integer size() { return instanceList.size(); };

	/*
	 * fromString
	 * converts string to StartTime
	 * fails if it finds zero or more than one
	 */
	public static StartTime fromString(String string) {
		Integer count = 0;
		StartTime theRtn = null;
		for (StartTime startTime : StartTime.asList()) {
			if (string.contains(startTime.name())) {
				theRtn = startTime;
				count++;
			}
		}
		// sanity check - there should be exactly one
		if (!count.equals(1)) {
			Msg.fatal(dS + ": expected exactly one but there were " + count + " string was " + string);
		}
		return theRtn;
	}


	/*
	 * map a StartTime into an AmPm
	 */
	public AmPm toAmPm() {

		if (
				this.equals(T_8_30) ||
				this.equals(T_10_00) ||
				this.equals(T_10_30) ||
				this.equals(T_11_30)
				) { return AmPm.AM; }
		if (
				this.equals(T_1_00) ||
				this.equals(T_2_30) ||
				this.equals(T_3_00) ||
				this.equals(T_4_00)
				) { return AmPm.PM; }

		// not found
		Msg.fatal(dS + ": unable to convert StartTime " + this.name() + " to an AmPm");
		return null;
	}

	public int toIndex() {
		if (this.equals(T_8_30))  return 0;
		if (this.equals(T_10_00)) return 1;
		if (this.equals(T_10_30)) return 2;
		if (this.equals(T_11_30)) return 3;
		if (this.equals(T_1_00))  return 4;
		if (this.equals(T_2_30))  return 5;
		if (this.equals(T_3_00))  return 6;
		if (this.equals(T_4_00))  return 7;
		Msg.fatal(dS + ": problem in toIndex - hit the default case");
		return 0;
	}		


	// debug
	private static String dS = "StartTime";
}

